﻿/// <reference path="jquery-1.11.0.js" />
/// <reference name="MicrosoftAjax.js"/>
///Quannm:
Common = function () {
    return {
        //Lấy keycode trong mọi trình duyệt
        getKeyCode: function (event, eventType) {
            if (window.event) {
                event = window.event;
            }
            if (eventType == "keypress")
                return event.charCode;
            else
                return event.keyCode;
        },
        //Vịt trị contro
        setlectRang: function (input, selectionStart, selectionEnd) {
            if (input.setSelectionRange) {
                input.focus();
                input.setSelectionRange(selectionStart, selectionEnd);
            }
            else if (input.createTextRange) {
                var range = input.createTextRange();
                range.collapse(true);
                ev
                range.moveStart('character', selectionStart);
                range.select();
            }
        },
        //Kiểm tra có phải là ký tự số ko
        isKeyDigit: function (keyCode) {
            return (0x30 <= keyCode && keyCode <= 0x39);
        },
        isKeyNavigation: function (keyCode) {
            return (Sys.UI.Key.left <= keyCode && keyCode <= Sys.UI.Key.down);
        },
        getCaret: function (el) {
            if (el.selectionStart) {
                return el.selectionStart;
            }
            else if (document.selection) {
                el.focus();
                var r = document.selection.createRange();
                if (r == null) {
                    return 0;
                }

                var re = el.createTextRange(),
                    rc = re.duplicate();
                re.moveToBookmark(r.getBookmark());
                rc.setEndPoint('EndToStart', re);
                return rc.text.length;
            }
            return 0;
        },
        Round: function (number, tp) {
            return Math.round(number * Math.pow(10, tp)) / Math.pow(10, tp);
        },
        onNextControl: function (control, e) {
            var b_form = document.forms[0], b_vtri = 0, b_tim = -1;
            for (var i = 0; i < b_form.elements.length - 1; i++) {
                if (b_tim < 0 && b_form.elements[i].type != 'hidden' && b_form.elements[i].focus && this.NVL(b_form.elements[i].style.display) != "none") {
                    b_tim = i;
                }

                if (control == b_form.elements[i]) {
                    b_vtri = i + 1;
                    break;
                }
            }

            for (var i = b_vtri; i < b_form.elements.length; i++) {
                if (b_form.elements[i].type != 'hidden' && b_form.elements[i].focus && this.NVL(b_form.elements[i].style.display) != "none") {
                    b_tim = i;
                    break;
                }
            }

            if (b_tim >= 0) {
                b_form.elements[b_tim].focus();
            }
        },
        onPreviousControl: function (control) {
            var b_form = document.forms[0], b_vtri = 0, b_tim = -1;
            for (var i = 0; i < b_form.elements.length - 1; i++) {
                if (b_tim < 0 && b_form.elements[i].type != 'hidden' && b_form.elements[i].focus && this.NVL(b_form.elements[i].style.display) != "none") {
                    b_tim = i;
                }

                if (control == b_form.elements[i]) {
                    b_vtri = i - 1;
                    break;
                }
            }

            for (var i = b_vtri; i < b_form.elements.length; i++) {
                if (b_form.elements[i].type != 'hidden' && b_form.elements[i].focus && this.NVL(b_form.elements[i].style.display) != "none") {
                    b_tim = i;
                    break;
                }
            }

            if (b_tim >= 0) {
                b_form.elements[b_tim].focus();
            }
            return false;
        },
        DaysOfMonth: function (moth, year) {
            var mon = parseInt(moth, 10);
            var yar = parseInt(year, 10);
            switch (mon) {
                case 2:
                    if ((yar % 4 == 0) && (yar % 400 != 0))
                        return 29;
                    else
                        return 28;
                    break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                    break;
                default:
                    return 30;
            }
        },
        // Kiểm tra xem chuỗi có chứa ký tự số không
        containsDigits: function (value) {
            return /\d/g.test(value);
        },
        formCodeToChar: function (code) {
            return String.fromCharCode(code);
        },
        isNullOrEmty: function (value) {
            if (value == null || value == "" || value == undefined)
                return true;
            else
                return false;
        },
        isNumber: function (input) {
            return /\d/g.test(input);
        },
        transformValue: function (currentVal, listVal) {
            if (this.isNullOrEmty(listVal)) return currentVal;
            var aVal = listVal.split(","), bPost = -1, i;
            for (i = 0; i < aVal.length; i++) {
                if (currentVal == aVal[i]) { bPost = i + 1; break; }
            }
            if (bPost < 0 || bPost >= aVal.length) bPost = 0;
            return aVal[bPost];
        },
        stringToJson: function (val) {
            if (val == "" || val == undefined) return "";
            return jQuery.parseJSON(val);
        },
        JsonToArray: function (json) {
            var name = [], val = [];
            var i = 0;
            $.each(json, function (index, value) {
                name[i] = index;
                val[i] = value;
                i++;
            });
            return [name, val];
        },
        NVL: function (val) {
            if (val == undefined || val==null) return "";
            else return val.trim();
        },
        ToBool: function (val) {
            return Boolean.parse(val);
        },
        ToInt: function (val) {
            return parseInt(val);
        },
        StringNumberToNumber: function (val) {
            if (val == "") return 0;
            while (val.indexOf(',') >= 0) val = val.replace(',', '');
            return val * 1.0;
        },
        isEmtyDate: function (val) {
            if (val == null) return true;
            val = this.NVL(val);
            while (val.indexOf('/') >= 0) val = val.replace('/', '');
            return (val == "");
        },
        StringDataToDate: function (val) {
            if (this.isEmtyDate(val)) val = "01/01/30000";
            var day = parseInt(val.substr(0, 2), 10), month = parseInt(val.substr(3, 2), 10), year = parseInt(val.substr(6, 4), 10);
            var result = new Date(year, month, day);
            return result;
        },
        DateToStringDate: function (val) {
            var day = b_ngay.getDate().toString(), month = (b_ngay.getMonth() + 1).toString();
            if (day.length < 2) day = "0" + day
            if (month.length < 2) month = "0" + month;
            return day + '/' + month + '/' + val.getFullYear().toString();
        },
        StringDateToNumber: function (val) {
            if (this.NVL(val) == "") return "0";
            return val.substr(6, 4) + val.substr(3, 2) + val.substr(0, 2);
        },
        StringToNumber: function (val, dec) {
            val =this.StringToFormatNumber(val, dec);
            if (val == "") return 0;
            while (val.indexOf(',') >= 0) val = val.replace(',', '');
            return val * 1.0;
        },
        StringToFormatNumber: function (val, dec) {
            if (val == null || val == "") return "";
            var b_moi = "", b_chan = "", b_tp = "", b_dau = "", b_cham = "", b_s, i;
            for (i = 0; i < val.length; i++) {
                b_s = val.substr(i, 1);
                if (b_s == ".") b_cham = ".";
                else if (b_s == "-") b_dau = "-";
                else if ("0123456789".indexOf(b_s) >= 0) {
                    if (b_cham == ".") dec = dec + b_s;
                    else if (b_chan == "0") b_chan = b_s;
                    else b_chan = b_chan + b_s;
                }
            }
            if (b_chan.length > 3) {
                while (b_chan != "") {
                    i = b_chan.length - 3;
                    if (i < 0) i = 0;
                    if (b_moi != "") b_moi = b_chan.substr(i) + "," + b_moi;
                    else b_moi = b_chan.substr(i);
                    if (i == 0) b_chan = ""; else b_chan = b_chan.substr(0, i);
                }
            }
            else b_moi = b_chan;
            if (dec != null)
                if (dec > 0 && b_tp.length > dec) b_tp = b_tp.substr(0, dec);
            b_moi = b_dau + b_moi + b_cham + b_tp;
            return b_moi;
        },
        paseData: function(data, column, max)
        {
            if (data.length > max) return data;
            for(var j=0; j< max-data.length; j++)
            {
                var obj = [];
                for( k=0; k< column.length; k++)
                {
                    obj[column[k].field] = "";
                }
                data.push(obj);
            }
        }
    };
}();


Msg = function () {
    return {
        Show: function (title, msg, icon) {
            if (icon == undefined || icon == "")
                $.messager.alert(title, msg);
            else if (icon == "info")
                $.messager.alert(title, msg, "info");
            else if (icon == "error")
                $.messager.alert(title, msg, "error");
            else if (icon == "question")
                $.messager.alert(title, msg, "question");
            else if (icon == "warning")
                $.messager.alert(title, msg, "warning");
        },
        Confirm: function (title, msg) {
            var a = $("<div></div>").append(msg)
            .dialog({
                autoOpen: false,
                bgiframe: true,
                width: 300,
                minWidth: 200,
                height: 150,
                closeOnEscape: true,
                modal: true,
                title: title,
                buttons: {
                    "Đồng ý": function () {
                        $(this).dialog("close");
                        return true;
                    },
                    "Hủy": function () {
                        $(this).dialog("close");
                        return false;
                    }
                }
            });
            a.dialog("open");
        },
        test: function (a) {
            alert("OK");
        },
        OpenWindow: function (title, url, width, heigth, event) {
            $('<div  class="easyui-dialog" style="width:' + width + 'px;height:' + heigth + 'px"></div>')
                .html('<iframe id="myFrame" width="100%" height="100%" frameborder="0" src=' + url + ' marginwidth="0" marginheight="0"></iframe>')
                .dialog({
                    title: title,
                    closed: false,
                    cache: false,
                    modal: true
                });
        }
    };
}();